// import { homestay_detail_api } from './../api/homestay_api.js'
import { order_scheduled_api } from './../api/order.js'
import { baseURL } from './../utils/http.js'

let homestay_id = ''
let start_date = ''
let end_date = ''
let homestay_detail = {}
let dates_str_arr=[]
function main(params) {
    init_render()
    submit_order()
}
main()
//获取民宿id
function init_render(params) {
    const search_param_arr = location.search.slice(1).split('&')
    homestay_id = search_param_arr[0].split('=')[1]
    start_date = search_param_arr[1].split('=')[1]
    end_date = search_param_arr[2].split('=')[1]
    get_homestay_detail()
    // homestay_detail_req()
    render_date_input()
}
//从sessionStorage中获取民宿详情
function get_homestay_detail(params) {
    homestay_detail = JSON.parse(sessionStorage.getItem('homestay_detail'))
    render_cost_breakdown()
    control_number_input_range(1, homestay_detail.people, '.check_in_person_btn input')
    render_homestay_info()
}
//请求民宿详情
function homestay_detail_req(params) {
    homestay_detail_api({ homestay_id }).then(res => {
        homestay_detail = res.data
        render_cost_breakdown()
        control_number_input_range(1, homestay_detail.people, '.check_in_person_btn input')
    })
}
//渲染头部民宿信息
function render_homestay_info(params) {
    $('.homestay_info').html(`
    <div class="homestay_info_left">
        <img src="${baseURL+homestay_detail.image}" alt="">
    </div>
    <div class="homestay_info_right">
        <span class="homestay_title">双井地铁口拎包入住可日租月租</span>
        <span class="homestay_label_group">
            ${homestay_detail.type_text}&nbsp;可住${homestay_detail.people}人
        </span>
    </div>`)
}
//渲染入住日期和退房日期
function render_date_input(params) {
    $('.check_in_input').val(start_date)
    $('.check_out_input').val(end_date)
}
//控制人数选择输入框所选区间范围
function control_number_input_range(min, max, selector) {
    $(selector).attr('min', min)
    $(selector).attr('max', max)
}
//渲染费用明细列表
function render_cost_breakdown(params) {
    const dates_arr = getDates(start_date, end_date)
    dates_str_arr = dates_arr.map(date => date.toISOString().split('T')[0]);
    // 结束日期不做计费所以删掉结束日期
    dates_str_arr.splice(dates_str_arr.length - 1, 1)
    //渲染全部房费
    let cost_all_template = `
        <div class="cost_all_left">全部房费（${dates_str_arr.length}天）</div>
        <div class="cost_all_right">￥${dates_str_arr.length * (homestay_detail.price)}</div>`
    $('.cost_all').html(cost_all_template)
    //渲染日期费用列表
    let cost_date_list_template = ''
    dates_str_arr.forEach((item, index) => {
        cost_date_list_template += `
        <li>
            <span class="date">${item}</span>
            <span class="cost">￥${homestay_detail.price}</span>
        </li>`
    })
    $('.cost_date_list').html(cost_date_list_template)
    //渲染总计
    $('.total_pay').text(`￥${dates_str_arr.length * (homestay_detail.price)}`)
}
//计算时间跨度
function getDates(start, end) {
    var startDate = new Date(start);
    var endDate = new Date(end);
    var dates = [];
    while (startDate <= endDate) {
        dates.push(new Date(startDate));
        startDate.setDate(startDate.getDate() + 1);
    }
    return dates;
}
//提交订单
function submit_order(params) {
    $('.submit_order').on('click', function () {
        const people = $('.check_in_person_btn input').val()
        const tel = $('.contacts .phone input').val()
        if (people && tel) {
            order_scheduled_api({
                room_id: homestay_id, 
                start_time: start_date, 
                end_time: end_date, people, 
                tel, 
                price: homestay_detail.price, 
                total: dates_str_arr.length * (homestay_detail.price)
            }).then(res => {
                location.assign('/index/Index/personal_center.html')
            }).catch((err) => {
                window.alert('提交失败')
            })
        } else {
            alert('请输入人数和联系电话')
        }
    })
}